package com.zzmltc.hosp.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.update.UpdateChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zzmltc.common.result.Result;
import com.zzmltc.common.utils.MD5;
import com.zzmltc.hosp.service.HospitalSetService;
import com.zzmstc.model.hosp.HospitalSet;
import com.zzmstc.vo.hosp.HospitalSetQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Select;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Collection;
import java.util.List;
import java.util.Random;


@Api(tags = "医院设置管理")
@Slf4j
@RestController
@RequestMapping("/admin/hosp/hospitalSet")
//@CrossOrigin
public class HospitalSetController {


    @Autowired
    private HospitalSetService hospitalSetService;

    @ApiOperation(value = "获取所有医院设置")
    @GetMapping("findAll")
    public Result findAllHospitalSet() {
        List<HospitalSet> list = hospitalSetService.list();
        log.info(list.toString());
        return Result.ok(list);
    }

    @ApiOperation(value = "逻辑删除医院设置")
    @DeleteMapping("{id}")
    public Result removeHospSet(@PathVariable Long id) {
        boolean removeById = hospitalSetService.removeById(id);
        if (removeById) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }

    //    条件查询带分页
    @ApiOperation(value = "带条件的分页展示")
    @PostMapping("findPageHospSet/{current}/{limit}")
    public Result findPageHospSet(@PathVariable long current,
                                  @PathVariable Long limit,
                                  @RequestBody(required = false) HospitalSetQueryVo hospitalSetQueryVo) {

//        创建page对象，传递当前页，每页记录数
        Page<HospitalSet> page = new Page<>(current, limit);

//        构造条件
        QueryWrapper<HospitalSet> wrapper = new QueryWrapper<>();

        String hosname = hospitalSetQueryVo.getHosname();
        String hoscode = hospitalSetQueryVo.getHoscode();

        if (!StringUtils.isEmpty(hosname)) {
            wrapper.like("hosname", hospitalSetQueryVo.getHosname());
        }
        if (!StringUtils.isEmpty(hoscode)) {
            wrapper.eq("hoscode", hospitalSetQueryVo.getHoscode());
        }

//        调用方法实现分页查询
        Page<HospitalSet> hospitalSetPage = hospitalSetService.page(page, wrapper);

//        返回结果
        return Result.ok(hospitalSetPage);
    }

//    添加医院设置
    @ApiOperation(value = "添加医院")
    @PostMapping("savaHospitalSet")
    public Result savaHospitalSet(@RequestBody HospitalSet hospitalSet) {
//        设置状态。 1可用 0不可用
        hospitalSet.setStatus(1);
        Random random = new Random();
        String encrypt = MD5.encrypt(System.currentTimeMillis() + "" + random.nextInt(1000));
        hospitalSet.setSignKey(encrypt);
        boolean save = hospitalSetService.save(hospitalSet);
//        调用service
        if (save) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }

    //    根据id获取医院设置
    @ApiOperation(value = "根据id获取医院数据")
    @GetMapping("getHospSet/{id}")
    public Result getHospSet(@PathVariable Long id) {
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        return Result.ok(hospitalSet);
    }

    //    修改医院设置
    @ApiOperation(value = "修改医院设置")
    @PutMapping("updataHospitalSet")
    public Result updataHospSet(@RequestBody HospitalSet hospitalSet) {
        boolean updateById = hospitalSetService.updateById(hospitalSet);
        if (updateById) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }
    @ApiOperation(value = "批量删除医院操作")
    @DeleteMapping("batchRemove")
    public Result batchRemoveHospitalSet(@RequestBody List<Long> idList) {
        boolean removeByIds = hospitalSetService.removeByIds(idList);
        if (removeByIds) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }

//    医院设置锁定和解锁
    @ApiOperation(value = "医院设置锁定和解锁")
    @PutMapping("lookHospitalSet/{id}/{status}")
    public Result lookHospitalSet(@PathVariable Long id,
                                  @PathVariable Integer status){
//        根据id查询医院信息
        HospitalSet serviceById = hospitalSetService.getById(id);

//        设置
        serviceById.setStatus(status);
//        调用方法
        boolean updateById = hospitalSetService.updateById(serviceById);
        if (updateById) {
            return Result.ok();
        } else {
            return Result.fail();
        }

    }
    @ApiOperation(value = "发送签名密钥")
    @PutMapping("sendKey/{id}")
    public Result lookHospitalSet(@PathVariable Long id){
        HospitalSet byId = hospitalSetService.getById(id);
        String signKey = byId.getSignKey();
        String hosname = byId.getHosname();
        return Result.ok();

    }


}
